# response.py
from typing import Any, Optional, Dict
from starlette.responses import JSONResponse


class JsResponse(JSONResponse):
    def __init__(self,
                 data: Optional[Any] = None,  # data 可以是 Any 类型，默认为 None
                 msg: str = 'Successful',
                 headers: Optional[Dict[str, str]] = None,
                 background: Optional[Any] = None
                 ) -> None:
        # 如果 data 为 None，则将其设置为一个空字典
        if data is None:
            data = []

            # 构造响应内容
        content = {
            'status': 0,
            'msg': msg,
            'data': data
        }

        # 调用父类的 __init__ 方法
        super().__init__(content=content, headers=headers, media_type='application/json',
                         background=background)


class CustomJsonFail(JSONResponse):
    """
    自定义的JSON响应类，用于表示失败的响应。
    """

    def __init__(self,
                 data: Optional[Any] = None,  # data 可以是 Any 类型，默认为 None
                 msg: str = 'Failed',  # 默认消息为 'Failed'
                 headers: Optional[Dict[str, str]] = None,
                 background: Optional[Any] = None
                 ) -> None:
        # 如果 data 为 None，则将其设置为一个空字典
        if data is None:
            data = []

            # 构造响应内容
        content = {
            'status': -1,
            'msg': msg,
            'data': data
        }

        # 调用父类的 __init__ 方法
        super().__init__(content=content, headers=headers, media_type='application/json',
                         background=background)


class UnicornException(Exception):
    def __init__(self, status: int = -1, errCode: int = 0, msg: str = '', data: str = ''):
        self.status = status
        self.errCode = errCode
        self.msg = msg
        self.data = data


class NotAuthorizedException(UnicornException):
    def __init__(self, msg: str = '无权限', data: str = ''):
        # 假设对于未授权异常，我们总是想设置状态为 -1（或某个其他特定值），错误码为某个特定值
        super().__init__(status=-1, errCode=403, msg=msg, data=data)
